package com.crm.api.service.impl;

import com.crm.api.entity.WorkOrderHistory;
import com.crm.api.mapper.WorkOrderHistoryMapper;
import com.crm.api.service.WorkOrderHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工单历史服务实现类
 */
@Slf4j
@Service
public class WorkOrderHistoryServiceImpl implements WorkOrderHistoryService {

    @Autowired
    private WorkOrderHistoryMapper workOrderHistoryMapper;

    @Override
    public void recordCreateHistory(Integer workOrderId, Integer operatorId, String operatorName) {
        WorkOrderHistory history = new WorkOrderHistory();
        history.setWorkOrderId(workOrderId);
        history.setOperationType(WorkOrderHistory.OPERATION_CREATE);
        history.setContent("工单创建");
        history.setOperatorId(operatorId);
        history.setOperatorName(operatorName);
        history.setCreatedAt(new Date());
        
        workOrderHistoryMapper.insert(history);
        log.info("记录工单创建历史: workOrderId={}, operator={}", workOrderId, operatorName);
    }

    @Override
    public void recordAssignHistory(Integer workOrderId, Integer oldAssignedTo, Integer newAssignedTo,
                                   Integer operatorId, String operatorName, String newAssignedName) {
        WorkOrderHistory history = new WorkOrderHistory();
        history.setWorkOrderId(workOrderId);
        history.setOperationType(WorkOrderHistory.OPERATION_ASSIGN);
        history.setOldValue(oldAssignedTo != null ? oldAssignedTo.toString() : "未分配");
        history.setNewValue(newAssignedTo != null ? newAssignedTo.toString() : "未分配");
        history.setContent("工单分配给: " + (newAssignedName != null ? newAssignedName : "未分配"));
        history.setOperatorId(operatorId);
        history.setOperatorName(operatorName);
        history.setCreatedAt(new Date());
        
        workOrderHistoryMapper.insert(history);
        log.info("记录工单分配历史: workOrderId={}, assignedTo={}", workOrderId, newAssignedName);
    }

    @Override
    public void recordStatusChangeHistory(Integer workOrderId, String oldStatus, String newStatus,
                                         Integer operatorId, String operatorName) {
        WorkOrderHistory history = new WorkOrderHistory();
        history.setWorkOrderId(workOrderId);
        history.setOperationType(WorkOrderHistory.OPERATION_STATUS_CHANGE);
        history.setOldValue(oldStatus);
        history.setNewValue(newStatus);
        history.setContent("状态从 '" + oldStatus + "' 更改为 '" + newStatus + "'");
        history.setOperatorId(operatorId);
        history.setOperatorName(operatorName);
        history.setCreatedAt(new Date());
        
        workOrderHistoryMapper.insert(history);
        log.info("记录工单状态变更历史: workOrderId={}, {} -> {}", workOrderId, oldStatus, newStatus);
    }

    @Override
    public void recordSolutionHistory(Integer workOrderId, String solution,
                                     Integer operatorId, String operatorName) {
        WorkOrderHistory history = new WorkOrderHistory();
        history.setWorkOrderId(workOrderId);
        history.setOperationType(WorkOrderHistory.OPERATION_ADD_SOLUTION);
        history.setNewValue(solution);
        history.setContent("添加解决方案");
        history.setOperatorId(operatorId);
        history.setOperatorName(operatorName);
        history.setCreatedAt(new Date());
        
        workOrderHistoryMapper.insert(history);
        log.info("记录工单解决方案历史: workOrderId={}", workOrderId);
    }

    @Override
    public void recordFeedbackHistory(Integer workOrderId, String feedback, Integer rating,
                                     Integer operatorId, String operatorName) {
        WorkOrderHistory history = new WorkOrderHistory();
        history.setWorkOrderId(workOrderId);
        history.setOperationType(WorkOrderHistory.OPERATION_ADD_FEEDBACK);
        history.setNewValue(feedback);
        history.setContent("添加客户反馈，满意度评分: " + (rating != null ? rating + "分" : "未评分"));
        history.setOperatorId(operatorId);
        history.setOperatorName(operatorName);
        history.setCreatedAt(new Date());
        
        workOrderHistoryMapper.insert(history);
        log.info("记录工单客户反馈历史: workOrderId={}, rating={}", workOrderId, rating);
    }

    @Override
    public List<Map<String, Object>> getWorkOrderHistory(Integer workOrderId) {
        return workOrderHistoryMapper.findByWorkOrderId(workOrderId);
    }

    @Override
    public List<Map<String, Object>> getWorkOrderSolutions(Integer workOrderId) {
        return workOrderHistoryMapper.findByWorkOrderIdAndType(workOrderId, WorkOrderHistory.OPERATION_ADD_SOLUTION);
    }

    @Override
    public List<Map<String, Object>> getWorkOrderStatusHistory(Integer workOrderId) {
        List<Map<String, Object>> statusHistory = workOrderHistoryMapper.findByWorkOrderIdAndType(workOrderId, WorkOrderHistory.OPERATION_CREATE);
        statusHistory.addAll(workOrderHistoryMapper.findByWorkOrderIdAndType(workOrderId, WorkOrderHistory.OPERATION_ASSIGN));
        statusHistory.addAll(workOrderHistoryMapper.findByWorkOrderIdAndType(workOrderId, WorkOrderHistory.OPERATION_STATUS_CHANGE));
        
        // 按时间排序
        statusHistory.sort((a, b) -> {
            Date dateA = (Date) a.get("createdAt");
            Date dateB = (Date) b.get("createdAt");
            return dateA.compareTo(dateB);
        });
        
        return statusHistory;
    }
}